//
// Created by wenbo on 24-9-7.
//

#ifndef __DRV_MRC_H__
#define __DRV_MRC_H__

#ifdef __cplusplus
extern "C"
{
#endif

#include "tim.h"
#include "usart.h"
#include "adc.h"


// #define AT8236
#define VNH7040

    typedef enum
    {
        Disengagement,
        Engagement,
        Demagnetization,
        Magnetization

    } MR_State;


    typedef enum
    {
        NONE = 0,
        UNLOCK = 1,
        LOCK = 2
    }MR_Command;

#pragma pack(1)
    /**
     * @brief Structure representing the MR device.
     *
     * This structure holds various parameters and function pointers related to the MR device.
     */
    typedef struct
    {
        MR_State state; // State of the MR device

        void (*init)(TIM_HandleTypeDef, uint32_t, uint32_t, uint16_t); // Function pointer to initialize the MR device
        const uint8_t *device_name;                                    // Pointer to the device name
        TIM_HandleTypeDef *pwm_handle;                                 // Pointer to the TIM_HandleTypeDef structure
    #ifdef AT8236
        uint32_t IN1;                                                  // Input 1
        uint32_t IN2;                                                  // Input 2
    #endif

    #ifdef VNH7040

        GPIO_TypeDef * INA;
        uint32_t INA_PIN;

        GPIO_TypeDef * INB;
        uint32_t INB_PIN;

        uint32_t PWM;                                                  // PWM
    #endif

        int (*LOG_MSG)(const char *format, ...);                       // Function pointer to log messages
        float voltage;                                                 // Voltage of the MR device
        uint8_t STATE_UPDATE_FLAG;                                     // Flag to update the state
        uint8_t demagnetization_counter;                               // Counter for demagnetization
        uint8_t magnetization_counter;

        MR_Command cmd;

        float real_voltage;                                 // Counter for magnetization
    } MRC_device;
#pragma pack()

    void MRC_set_pwm_param(TIM_HandleTypeDef *htim, uint32_t Channel, uint32_t freq, uint16_t duty);

#ifdef AT8236
    __WEAK void MRC_Init(const uint8_t *dev_name, MRC_device *MRC, TIM_HandleTypeDef *htim, uint32_t IN1, uint32_t IN2);
#endif

#ifdef VNH7040
    void MRC_Init(const uint8_t *dev_name, MRC_device *MRC, TIM_HandleTypeDef *htim,
                GPIO_TypeDef * INA,  uint16_t INA_PIN,
                GPIO_TypeDef * INB,  uint16_t INB_PIN,
                uint32_t PWM);
    void VNH7070_Multisense_ADC_process(MRC_device *MRC1, MRC_device *MRC2);
#endif

    void MRC_set_voltage(float voltage, MRC_device *MRC);
    void MRC_lock(MRC_device *MRC);
    void MRC_unlock(MRC_device *MRC);
    void MRC_switch(MRC_device *MRC, TIM_HandleTypeDef *htim);

#define DEMAGNETIZATION_BUFFER_SIZE 255 // Define the size of the demagnetization buffer
#define MAGNETIZATION_BUFFER_SIZE 255   // Define the size of the magnetization buffer

    extern float demagnetization_buffer[];
    extern float magnetization_buffer[];
    // extern MRC_device MRC1;
    // extern MRC_device MRC2;

#ifdef __cplusplus
}
#endif

#endif
